package org.ftc.dao;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.ftc.domain.CommitteeTeam;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository
public class CommitteeTeamDao extends CurrentDao <CommitteeTeam, String> {

	@Override
	public CommitteeTeam load(String id) {
		return super.load(id);
	}

	@Override
	public CommitteeTeam get(String id) {
		return super.get(id);
	}
	
	@SuppressWarnings("unchecked")
	public CommitteeTeam getChecked() {
		try{	
			Criteria criteria = getSession().createCriteria(CommitteeTeam.class); 
			criteria.createAlias("emMapping", "ctMap");
			criteria.add(Restrictions.eq("enable", true));
			List<CommitteeTeam> list =(List<CommitteeTeam>) criteria.list();
			 return CollectionUtils.isEmpty(list)?null:list.get(0);
		}catch( Exception e ){
			getLog().error(e.toString());
			return null; 
		}
	}

	@Override
	public synchronized boolean save(CommitteeTeam entity) {
		return super.save(entity);
	}

	@Override
	public synchronized boolean saveOrUpdate(CommitteeTeam entity) {
		return super.saveOrUpdate(entity);
	}

	@Override
	public synchronized boolean update(CommitteeTeam entity) {
		return super.update(entity);
	}

	@Override
	public synchronized boolean delete(CommitteeTeam entity) {
		try {
			String hql = "delete from CTMap where T_CODE = :tCode";
			getSession().createQuery(hql).setString("tCode", entity.gettCode()).executeUpdate();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
	}

	@Override
	public synchronized boolean delete(String id) {
		return super.delete(id);
	}
	
	@Override
	public List<CommitteeTeam> findPage(int offset, int pageSize) {
		return super.findPage(offset, pageSize);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CommitteeTeam> findAll() {
		try{	
			Criteria criteria = getSession().createCriteria(CommitteeTeam.class); 
			criteria.addOrder(Order.desc("date"));
			return (List<CommitteeTeam>) criteria.list();
		}catch( Exception e ){
			getLog().error(e.toString());
			return null; 
		}
	}

	@Override
	public long countAll() {
		return super.countAll();
	}


}
